package com.example.chenjun.myapplication.net;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.ContextThemeWrapper;

import com.example.chenjun.myapplication.net.netcallback.BaseCallback;
import com.example.chenjun.myapplication.net.netcallback.ParseCallback;

import java.io.File;

public abstract class BaseLoader<T> {
    protected NetController mController;
    private Dialog mLoadingDialog;
    private Activity mDialogActivity;
    private boolean isLoading;

    public BaseLoader(NetController<T> controller){
        mController = controller;
    }

    public BaseLoader(NetController<T> controller, Dialog loadingDialog){
        this(controller);
        mLoadingDialog = loadingDialog;
        suitActivity(mLoadingDialog);
    }

    private void suitActivity(Dialog loadingDialog) {
        Context context = loadingDialog.getContext();
        if (context instanceof Activity){
            mDialogActivity = (Activity) context;
            return;
        }
        Context baseContext = ((ContextThemeWrapper) context).getBaseContext();
        if (baseContext instanceof Activity){
            mDialogActivity = (Activity) baseContext;
        }
    }

    public abstract T execute(ParseCallback<T> parseCallback);

    public abstract void enqueue(BaseCallback<T> callback);

    public abstract File downFile();

    public abstract void cancel();

    public abstract void retry();

    public Activity getDialogActivity() {
        return mDialogActivity;
    }

    protected void showLoadingDialog(){
        if (mLoadingDialog != null){
            mLoadingDialog.show();
        }
    }
    protected void hideLoadingDialog(){
        if (mLoadingDialog != null){
            mLoadingDialog.dismiss();
        }
    }
}
